package com.lonely.api.shein.service;

import cn.hutool.core.lang.Assert;
import com.lonely.api.shein.config.HttpClientConfig;
import com.lonely.api.shein.exception.OpenSdkException;
import com.lonely.api.shein.http.HttpClientProxy;
import com.lonely.api.shein.http.RequestBuilder;
import com.lonely.api.shein.model.AuthInfo;
import com.lonely.api.shein.util.AesUtil;

/**
 * shein sdk客户端接口实现
 *
 * @author fzzf
 */
public class SheinOpenSDKClientImpl implements SheinOpenSDKClient {

    /**
     * http 客户端代理
     */
    private final HttpClientProxy httpClient;

    public SheinOpenSDKClientImpl(HttpClientConfig config) {
        this.httpClient = new HttpClientProxy(config);
    }

    /**
     * 根据临时token获取 openKeyId和secretKey
     *
     * @param tempToken tempToken
     * @param authInfo
     * @return
     * @throws OpenSdkException
     */
    @Override
    public String getToken(String tempToken, AuthInfo authInfo) throws OpenSdkException {
        return httpClient.getByToken(tempToken, authInfo);
    }


    /**
     * 发送http get请求
     *
     * @param requestBuilder Request Builder
     * @return
     * @throws OpenSdkException
     */
    @Override
    public String get(RequestBuilder requestBuilder) throws OpenSdkException {
        return httpClient.doExecute(requestBuilder);
    }


    /**
     * 发送http post请求
     *
     * @param requestBuilder Request Builder
     * @return
     * @throws OpenSdkException
     */
    @Override
    public String post(RequestBuilder requestBuilder) throws OpenSdkException {
        return httpClient.doExecute(requestBuilder);
    }


    /**
     * 临时token调用返回的SecretKey 需要解密
     *
     * @param encryptedSecretKey 待解密的数据
     * @param appSecret
     * @return
     */
    @Override
    public String decryptSecretKey(String encryptedSecretKey, String appSecret) {
        Assert.notBlank(encryptedSecretKey, "encryptedSecretKey not be blank");
        Assert.notBlank(appSecret, "appSecret not be blank");
        return AesUtil.decrypt(encryptedSecretKey, appSecret);
    }


    /**
     * 解密事件数据
     *
     * @param encryptedEventData encrypted data
     * @param appSecret
     * @return
     */
    @Override
    public String decryptEventData(String encryptedEventData, String appSecret) {
        Assert.notBlank(encryptedEventData, "encryptedEventData not be blank");
        Assert.notBlank(appSecret, "appSecret not be blank");
        return AesUtil.decrypt(encryptedEventData, appSecret);
    }
}
